<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>随笔主页</title>
    <link rel="stylesheet" href="css/list.css">
    <link rel="stylesheet" href="css/blog_list.css">
    <style>
        .nav{
            position: fixed;
            top: 0;
            left: 0;
            right: 0;
            height: 50px;
        }
        /* 博客主页导航栏中的占位器 */
        .nav #myspacer {
            width: 40%;
        }
        .container{
            padding-top: 80px;
            height: auto;
        }
        .container-right{
            width: auto;
        }
        .blog-pagnation-wrapper{
            height: 40px;
            margin: 16px 0;
            text-align: center;
        }
        .blog-pagnation-item{
            display: inline-block;
            padding: 8px;
            border: 1px solid #d0d0d5;
            color: #333;
        }
        .blog-pagnation-item:hover{
            background: #4e4eeb;
            color: #fff;
        }
        .blog-pagnation-item.actvie{
            background: #4e4eeb;
            color: #fff;
        }
        /* 搜索框和按钮的样式 */
        #searchspan {
            display: flex;
            margin-bottom: 10px;
        }
        #search {
            height: 40px;
            margin-top: 10px;
            width: 200px;
            text-indent: 10px;
            border-radius: 10px;
            outline: none;
            border: none;
            background-color: rgba(255, 255, 255, 0.8);
        }
        #mysearchbt {
            display: inline-block;
            justify-content: center;
            width: 60px;
            height: 40px;
            line-height: 40px;
            border-radius: 10px;
            color: white;
            background-color: #fa8497;
            text-align: center;
            text-decoration: none;
            margin-top: 10px;
            border: 2px solid #f05972;
            /* 给颜色加上过渡效果 */
            transition: all 0.3s;
        }
        /* 查询按钮文字效果 */
        #mysearchbt span {
            display: inline-block;
            text-align: center;
            font-weight: bold; /* 设置文字加粗 */
        }
        #mysearchbt:hover {
            background-color: #f21539;
            color: #fff;
        }

    </style>
    <script src="js/common.js"></script>
    <script src="js/jquery.min.js"></script>
</head>

<body>
    <!-- 导航栏 -->
    <div class="nav">
        <img src="img/logo2.jpg" alt="">
        <span class="title">个人随笔分享平台</span>
        <!-- 用来占据中间位置 -->
        <span id="myspacer"></span>
        <!-- 搜索框 -->
        <span id="searchspan">
            <input id="search" type="text" placeholder="搜索一下, 你就知道"/>&nbsp;<button id="mysearchbt" onclick="myclick()"><span>搜索</span></button>
        </span>
        <a href="blog_list.html">随笔主页</a>
        <a href="blog_add.html">写随笔</a>
        <a href="myblog_list.html">我的随笔</a>
        <a href="my_draftbox.html">草稿箱</a>
        <a href="login.html">登录</a>
        <!-- <a href="#">注销</a> -->
    </div>
    <!-- 版心 -->
    <div class="container">
        <!-- 右侧内容详情 -->
        <div class="container-right" style="width: 100%;">
            <div id="artDiv">
                <!-- 每一篇博客包含标题, 摘要, 时间 -->
            </div>
            <div class="blog-pagnation-wrapper">
                <button class="blog-pagnation-item" onclick="firstPage()">首页</button> 
                <button class="blog-pagnation-item" onclick="lastPage()">上一页</button>
                <button class="blog-pagnation-item" onclick="nextPage()">下一页</button>
                <button class="blog-pagnation-item" onclick="finalPage()">末页</button>
                当前页码:&nbsp;<span id="pindex"></span>
                总页数:&nbsp;<span id="pcount"></span>
            </div>
        </div>
    </div>
    <script>
        // 当前页码, 默认第一页
        var pindex = getUrlValue("pindex") == "" ? 1 : getUrlValue("pindex");
        var pcount = 0; // 总页数， 默认为0
        var psize = 5; // 每页最多显示的条数
        // 搜索字段
        var key = getUrlValue("key") == "" ? "" : getUrlValue("key");

        // 初始化页面
        function initPage() {
            // 先渲染必要组件
            var pindexSpan = jQuery("#pindex");
            var pcountSpan = jQuery("#pcount");
            pindexSpan.html(pindex);
            pcountSpan.html(pcount);
            // 向后端请求数据, 构造博客列表与页码
            jQuery.ajax({
                url:"/art/listbypage",
                type:"POST",
                data:{"pindex":pindex,"psize":psize, "key":key},
                success:function(result) {
                    if(result != null && result.code == 200 
                         && result.data.articleInfoList != null 
                         && result.data.articleInfoList.length > 0) {
                        // 更新总页码
                        if(result.data.totalArticleCount != null
                            && result.data.totalArticleCount > 0) {
                            pcountSpan.html(Math.ceil(result.data.totalArticleCount / psize));
                        }
                        var artListDiv = "";
                        // 构造文章列表
                        for(var i = 0; i < result.data.articleInfoList.length; i++) {
                            // 得到每个文章对象
                            var artItem = result.data.articleInfoList[i];
                            artListDiv += '<div class="blog">';
                            artListDiv += '<div class="title">' + artItem.title + '</div>';
                            artListDiv += '<div class="date">' + artItem.updatetime + '</div>';
                            artListDiv += '<div class="desc">';
                            artListDiv += artItem.content;
                            artListDiv += '</div>';
                            artListDiv += '<div class="mydiv"><span>';
                            artListDiv += '<a href="blog_content.html?id='+
                            artItem.id +'" class="detail">查看全文 &gt;&gt;</a>&nbsp&nbsp';
                            artListDiv += '</span></div>';
                            artListDiv += '</div>';
                        }
                        // 添加到页面上
                        jQuery("#artDiv").html(artListDiv);
                    } else {
                        // 未查询到结果
                        jQuery("#artDiv").html('&nbsp;&nbsp;<h3>暂未查询到相关随笔!<a href="/blog_list.html">点击回到随笔主页</h3>');
                    }
                }
            });
        }
        initPage();
        // 首页
        function firstPage() {
            pindex = 1;
            location.href = "/blog_list.html" + "?pindex=" + pindex + "&key=" + key;
        }
        // 上一页
        function lastPage() {
            if (pindex <= 1) {
                alert("已经到首页啦!");
                return;
            }
            pindex = parseInt(pindex) - 1;
            location.href = "/blog_list.html" + "?pindex=" + pindex + "&key=" + key;
        }
        // 下一页
        function nextPage() {
            if (pindex >= parseInt(jQuery("#pcount").html())) {
                alert("已经到尾页啦!");
                return;
            }
            pindex = parseInt(pindex) + 1;
            location.href = "/blog_list.html" + "?pindex=" + pindex + "&key=" + key;
        }
        // 末页
        function finalPage() {
            pindex = parseInt(jQuery("#pcount").html());
            location.href = "/blog_list.html" + "?pindex=" + pindex + "&key=" + key;
        }
        // 搜索按钮
        function myclick() {
            pindex = 1;
            key = jQuery("#search").val();
            location.href = "/blog_list.html" + "?pindex=" + pindex + "&key=" + key;
        }
    </script>
</body>
</html>